﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

public class UITouchEffect : MonoBehaviour
{
    private string _uiEffectPath = "Assets/ResourcesAssets/Prefabs/Effect/UI/sfx_ui_gta_dianji_02.prefab"; 
    /// <summary>
    /// 屏幕特效初始缓存
    /// </summary>
    private GameObject fxSample;

    /// <summary>
    /// 屏幕特效的生命时长
    /// </summary>
    public float fxLifeTime = 1.0f;

    /// <summary>
    /// 屏幕特效的父节点
    /// </summary>
    public Transform fxContainer;

    /// <summary>
    /// 渲染相机
    /// </summary>
    public Camera fxRenderCamera;

    private ResLoader _loader;

    private Queue<GameObject> pool = new Queue<GameObject>(100);
    private List<GameObject> activatedFXList = new List<GameObject>();
    private RectTransform effectRoot;
    public void initEffect(Camera camera, Transform root)
    {
        fxRenderCamera = camera;
        fxContainer = root;
        Transform effectRootTf = new GameObject("UITouchEffectRoot").transform;
        effectRootTf.SetParent(root, false);
        effectRoot = effectRootTf.gameObject.AddComponent<RectTransform>();
        effectRoot.anchorMin = new Vector2(0, 0);
        effectRoot.anchorMax =new Vector2(1, 1);
        effectRoot.pivot =new Vector2(0.5f, 0.5f);
        effectRoot.sizeDelta =new Vector2(0, 0);
        
        if (fxSample == null)
        {
            _loader = ResLoader.Alloc();
            _loader.Add2Load(_uiEffectPath, typeof(GameObject), (success, assetName, asset) =>
            {
                if (success)
                {
                    fxSample = asset as GameObject;
                    this.enabled = true;
                }
                else
                {
                    Debug.LogWarning("没有找到屏幕特效");
                }
            });
            _loader.Load();
        }
    }

    private void Update()
    {
        if (fxSample == null) return;
        for (int i = activatedFXList.Count - 1; i >= 0; --i)
        {
            GameObject fx = activatedFXList[i];
            float fxTime = float.Parse(fx.name);
            if (Time.time - fxTime > fxLifeTime)
            {
                RecycleFX(fx);
                activatedFXList.RemoveAt(i);
            }
        }

        if (Application.isMobilePlatform)
        {
            for (int i = 0; i < Input.touchCount; ++i)
            {
                Touch touch = Input.GetTouch(i);
                if (touch.phase == TouchPhase.Began)
                {
                    PlayFX(touch.position);
                }
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                PlayFX(Input.mousePosition);
            }
        }
    }


    private void PlayFX(Vector2 tapPos)
    {
        GameObject fx = CreateFX();
        fx.name = Time.time.ToString();
        activatedFXList.Add(fx);
        
        RectTransform fxRectTrans = fx.GetComponent<RectTransform>();
        Vector2 fxLocalPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(effectRoot, tapPos, fxRenderCamera, out fxLocalPos);
        fxRectTrans.SetParent(effectRoot.transform,false);
        fxRectTrans.anchoredPosition3D = fxLocalPos;
        fx.SetActive(true);
    }


    private GameObject CreateFX()
    {
        GameObject newFX = null;
        if (pool.Count > 0)
        {
            newFX = pool.Dequeue();
        }
        else
        {
            newFX = Instantiate(fxSample);
        }
        return newFX;
    }

    private void RecycleFX(GameObject fx)
    {
        fx.SetActive(false);
        pool.Enqueue(fx);
    }
}
